package com.star.modules.sys.api;

import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mapper.BeanMapper;
import com.jeesite.modules.sys.utils.DictUtils;
import com.star.modules.sys.base.BizDataResult;
import com.star.modules.sys.entity.ShHouse;
import com.star.modules.sys.entity.ShInfo;
import com.star.modules.sys.enums.DictEnum;
import com.star.modules.sys.enums.ResultEnum;
import com.star.modules.sys.excetion.ApiException;
import com.star.modules.sys.service.InforMationService;
import com.star.modules.sys.service.ShHouseService;
import com.star.modules.sys.service.ShInfoService;
import com.star.modules.sys.vo.ArticleInfoVo;
import com.star.modules.sys.vo.IndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 首页接口类
 */
@RestController
@RequestMapping(value = "${frontPath}/sys/info")
public class InforMationController {

    @Autowired
    private InforMationService inforMationService;

    @Autowired
    private ShInfoService shInfoService;

    @Autowired
    private ShHouseService shHouseService;

    private static final String CONTACT_ID = "2";

    private static final String ABOUT_ID = "1";

    private static final int SIDE_EDGE = 5;

    /**
     * 首页展示信息
     *
     * @return
     */
    @RequestMapping(value = "listInfo")
    public BizDataResult getIndexInfo(ShInfo shInfo, Page page) {
        return inforMationService.getInforMation(shInfo, page);
    }

    /**
     * 联系我们
     *
     * @return
     */
    @RequestMapping(value = "contactInfo")
    public BizDataResult getContactInfo() {
        String content = "";
        try {
            ShInfo shInfo = shInfoService.get(CONTACT_ID);
            content = shInfo.getContent();
        } catch (Exception e) {
            throw new ApiException(e);
        }
        return BizDataResult.createResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), content);
    }

    /**
     * 关于我们
     *
     * @return
     */
    @RequestMapping(value = "aboutInfo")
    public BizDataResult getAboutInfo() {
        String content = "";
        try {
            ShInfo shInfo = shInfoService.get(ABOUT_ID);
            content = shInfo.getContent();
        } catch (Exception e) {
            throw new ApiException(e);
        }
        return BizDataResult.createResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), content);
    }

    /**
     * 详情
     *
     * @return
     */
    @RequestMapping(value = "articleInfo")
    public BizDataResult getArticleInfo(String id) {
        ArticleInfoVo articleInfoVo = null;
        try {
            ShInfo shInfo = shInfoService.get(id);
            shInfo.setVisitNum(shInfo.getVisitNum() + 1);
            ShInfo updateShinfo = new ShInfo();
            updateShinfo.setId(shInfo.getId());
            updateShinfo.setVisitNum(shInfo.getVisitNum());
            shInfoService.update(updateShinfo);
            articleInfoVo = BeanMapper.map(shInfo, ArticleInfoVo.class);
            articleInfoVo.setMenuColStr(DictUtils.getDictLabel(DictEnum.INFO_TYPE.getCode()
                    , shInfo.getMenuCol() + "", "false"));
        } catch (Exception e) {
            throw new ApiException(e);
        }
        return BizDataResult.createResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), articleInfoVo);
    }

    /**
     * 详情
     *
     * @return
     */
    @RequestMapping(value = "getSideEdge")
    public BizDataResult getSideEdge() {
        Map<String, Object> sideData = MapUtils.newHashMap();
        try {
            ShHouse shHouse = new ShHouse();
            shHouse.setIsHot(Boolean.TRUE);
            shHouse.setPage(new Page<ShHouse>(1, SIDE_EDGE));
            sideData.put("houses",BeanMapper.mapList(shHouseService.findList(shHouse),IndexVo.HouseVo.class));
            ShInfo shInfo = new ShInfo();
            shInfo.setIsHot(Boolean.TRUE);
            shInfo.setPage(new Page<ShInfo>(1, SIDE_EDGE));
            sideData.put("infos",BeanMapper.mapList(shInfoService.findList(shInfo),IndexVo.InfoVo.class));
        } catch (Exception e) {
            throw new ApiException(e);
        }
        return BizDataResult.createResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), sideData);
    }
}
